﻿package  
{
	
	import flash.display.MovieClip;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.display.SimpleButton;
	
	
	public class ItemButton extends MovieClip 
	{
		public var loader:Loader;
		
		public var num:int = 0;
		public var id:int = -1;
		public var img:int = -1;
		
		public function ItemButton() 
		{
			// constructor code
			
			loader = new Loader();
			loader.mouseChildren = false;
			loader.mouseEnabled = false;
			addChild( loader );
			
			addChild( numText );
			numText.mouseEnabled = false;
			selectBtn.visible = false;
			
			btn.doubleClickEnabled = true;
		}
		
		public function updateItem( id:int , num:int , img:int , px:int , py:int ):void
		{
			this.num = num;
			
			if ( num > 1 )
			{
				numText.text = num.toString()
			}
			else
			{
				numText.text = "";
			}
			
			if ( id != this.id )
			{
				setItem( id , num , img , px , py );
			}
		}
		
		public function setItem( id:int , num:int , img:int , px:int , py:int ):void
		{
			this.id = id;
			this.num = num;
			this.img = img;
			
			if ( num > 1 )
			{
				numText.text = num.toString()
			}
			else
			{
				numText.text = "";
			}
			
			loader.unload();
			loader.load( new URLRequest( "../textureItem/" + img.toString() + ".png" ) );
			loader.x = px + 1;
			loader.y = py - 1;
		}
		
		public function select( b:Boolean ):void
		{
			selectBtn.visible = b;
		}
		
		public function clear():void
		{
			selectBtn.visible = false;
			
			loader.unload();
			
			numText.text = "";
			
			num = 0;
			id = -1;
			img = -1;
		}
		
	}
	
}
